﻿using System.Collections.Generic;
using Romulus;
using Romulus.Nes;
using SolKeyTest.Data;

namespace SolKeyTest
{
    /// <summary>
    /// Demonstrates 
    /// </summary>
    public class TileScreen:ScreenControl
    {
        // Helper to expose Rom as our specific ROM type.
        internal new SolRom Rom { get { return base.Rom as SolRom; } set { base.Rom = value; } }

        // This is where we prepare to render.
        protected override void BeforeRender() {
            base.BeforeRender();

            // This is where you should set you bg patterns (and sprite patterns, if applicable)
            Patterns = Rom.bgPatterns[0].PatternImage;

        }

        protected override void LoadBgPalette(ReturnEventArgs<CompositePalette> e) {
            base.LoadBgPalette(e);

            e.ReturnValue = palette;
        }
        protected override void LoadSpritePalette(ReturnEventArgs<CompositePalette> e) {
            base.LoadSpritePalette(e);
            // This is where we would return the sprite palette
        }
        
        protected override void ConstructNametable() {
            base.ConstructNametable();

            IList<TileEntry[,]> tiles = new List<TileEntry[,]>();
            tiles.Add(SolKeyTiles.Door);
            tiles.Add(SolKeyTiles.Key);
            tiles.Add(SolKeyTiles.MagicSeal_1);
            tiles.Add(SolKeyTiles.EvilMirror);

            // In the 2 x 2 area, paint the tiles we can use to drop on the screen
            for (int i = 0; i < tiles.Count / 2; i++)
            {
                for (int j = 0; j < tiles.Count / 2; j++)
                {
                    for (int tX = 0; tX < 2; tX++)
                    {
                        for (int tY = 0; tY < 2; tY++)
                        {
                            NameTable[i * 2 + tX, j * 2 + tY] = tiles[i + j * 2][tY, tX];
                        }
                    }
                }
            }
        }
        protected override void ConstructSpriteList(EventArgs<NameTableRenderer> e) {
            // This is where you would construct your sprite list
            // (You can safely ignore e and use the this.NameTable property)
            base.ConstructSpriteList(e);
        }


        /// <summary>
        /// Sends a signal back to the main form that the current active block is the selected tile.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            base.OnMouseDown(e);
        }

        #region Static
        // This data would normally come from the rom

        static byte[] spritePaletteData = new byte[] {
            0x0, 0x26, 0x29, 0x30, 
            0x0, 0x30, 0x16, 0x27, 
            0x0, 0x16, 0x10, 0x30, 
            0x0, 0x2c, 0x26, 0x38
        };

        static byte[] paletteData = new byte[] { 
            0xF, 0x7, 0x10, 0x30, 
            0xF, 0x7, 0x27, 0x30, 
            0xF, 0x7, 0x2c, 0x30, 
            0xF, 0x7, 0x27, 0x38
        };
        static CompositePalette palette;
        static CompositePalette spritePalette;

        static TileScreen() {
            palette = new CompositePalette(paletteData, 0);
            spritePalette = new CompositePalette(spritePaletteData, 0);
        }
        #endregion
    }
}
